﻿class LoaderManager extends egret.EventDispatcher {

    public init() {
        this.keyDic = new Array<any>();
        this.loaderDic = new Array<any>();
        this.callbackDic = new Array<any>();
        this.resDic = {};
        this.errorHandler = null;

    }

    public append(key: string, format: string, callback?: Function): void {

        /*
        if (format == egret.URLLoaderDataFormat.SOUND) {
            this.resDic[key] = QNService.getFullUrl(key);

            return;
        }
        */
        if (!key) 
            return;
        
        var loader = new egret.URLLoader();
        loader.dataFormat = format;

        loader.addEventListener(egret.Event.COMPLETE, this.onComp, this);
        loader.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onError, this);

        this.keyDic.push(key);
        this.loaderDic.push(loader);
        this.callbackDic.push(callback);
    }

    private loadedCount = 0;

    public startLoad(): void {
        if (this.keyDic.length == 0)
            this.dispatchEvent(new egret.Event(egret.Event.COMPLETE));
        else
            for (var i = 0; i < this.keyDic.length; i++) {
                var key = this.keyDic[i];
                var req = new egret.URLRequest(QNService.getFullUrl(key));

                var loader: egret.URLLoader = this.loaderDic[i];
                console.log("startload: " + key);
                loader.load(req);
            }
    }

    private onComp(event: egret.Event): void {
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        var index = this.loaderDic.indexOf(loader);

        var key = this.keyDic[index];

        console.log("finishload: " + key);
        this.resDic[key] = loader.data;
        var callback = this.callbackDic[index];
        if (callback != null)
            callback();
        this.loadedCount++;
        if (this.loadedCount == this.keyDic.length)
            this.dispatchEvent(new egret.Event(egret.Event.COMPLETE));
    }

    private onError(event: egret.IOErrorEvent): void {
        return;
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        var index = this.loaderDic.indexOf(loader);

        var key = this.keyDic[index];
        console.log(key + " is loadError");

        this.resDic[key] = loader.data;
        var callback = this.callbackDic[index];
        if (callback != null)
            callback();
        this.loadedCount++;
        if (this.loadedCount == this.keyDic.length)
            this.dispatchEvent(new egret.Event(egret.Event.COMPLETE));
        return;

        if (this.errorHandler != null)
            this.errorHandler();
        this.dispose();
    }

    public dispose(): void {
        for (var i = 0; i < this.loaderDic.length; i++) {
            var loader: egret.URLLoader = this.loaderDic[i];
            loader.removeEventListener(egret.Event.COMPLETE, this.onComp, this);
            loader.removeEventListener(egret.IOErrorEvent.IO_ERROR, this.onError, this);
            loader = null;
        }
        this.init();
    }

    private keyDic: any[];
    private loaderDic: any[];
    private callbackDic: any[];
    private resDic: any;

    public errorHandler: Function;

    public getRes(key: string): any {
        return this.resDic[key];
    }
} 